package com.usian.wemedia.service.impl;

import com.usian.common.dtas.BaseServiceImpl;
import com.usian.common.fastdfs.FastDFSClient;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.pojos.WmMaterial;
import com.usian.model.media.pojos.WmUser;
import com.usian.utils.threadlocal.WmThreadLocalUtils;
import com.usian.wemedia.mapper.WmMaterialMapper;
import com.usian.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

@Service
public class WmMaterialServiceImpl extends BaseServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
    @Autowired
    private FastDFSClient fastDFSClient;
    @Value("${fdfs.url}")
    private String fdfsUrl;

    @Override
    public ResponseResult upload(MultipartFile file) {
        //1.图片校验
        if (file == null) return ErrorParam;
        String file1 = null;
        try {
            file1 = fastDFSClient.uploadFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //从二级过滤拿到user对象
        WmUser user = WmThreadLocalUtils.getUser();
        //保存素材图片
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(user.getId());
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setUrl(file1 + fdfsUrl);
        wmMaterial.setType((short) 0);
        wmMaterial.setCreatedTime(new Date());
        save(wmMaterial);


        return ResponseResult.okResult(wmMaterial);
    }
}
